package com.igeek.health.service;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.igeek.health.dto.PageDTO;
import com.igeek.health.entity.CheckgroupCheckitemExample;
import com.igeek.health.entity.CheckgroupCheckitemKey;
import com.igeek.health.entity.Checkitem;
import com.igeek.health.entity.CheckitemExample;
import com.igeek.health.mapper.CheckgroupCheckitemMapper;
import com.igeek.health.mapper.CheckitemMapper;
import com.igeek.health.result.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Description 检查项业务逻辑
 * @Author chenmin
 * @Version 1.0
 * @Date 2023/2/14 15:35
 */
@Service
public class CheckItemService {

    @Autowired
    private CheckitemMapper checkitemMapper;
    @Autowired
    private CheckgroupCheckitemMapper checkgroupCheckitemMapper;

    //查询所有检查项
    @Transactional(readOnly = true)
    public List<Checkitem> findAll() {
        List<Checkitem> checkitems = checkitemMapper.selectByExample(null);
        return checkitems;
    }

    //根据条件，实现检查项分页查询
 /*   @Transactional(readOnly = true)
    public PageResult findPage(PageDTO pageDTO) {
        //MyBatis分页插件，实现分页查询
        Page<Object> page = PageHelper.startPage(pageDTO.getCurrentPage(), pageDTO.getPageSize());
        //封装查询条件
        CheckitemExample example = new CheckitemExample();
        String query = pageDTO.getQueryString();
        if(StringUtils.hasLength(query)){
            //or code = ?
            example.or().andCodeEqualTo(query);
            //or name = ?
            example.or().andNameEqualTo(query);
        }
        //执行分页查询  select * from checkitem where code = ? or name = ?
        List<Checkitem> list = checkitemMapper.selectByExample(example);
        return new PageResult(page.getTotal() , list);
    }*/

    // select * from checkitem where code=? or name=?
    @Transactional
    public PageResult findPage(PageDTO pageDTO) {
        //1.利用分页插件，实现查询 第一个参数当前页页码 第二个参数每页总记录数
        Page<Object> page = PageHelper.startPage(pageDTO.getCurrentPage(), pageDTO.getPageSize());
        //2.封装查询条件
        CheckitemExample example = new CheckitemExample();
        String query = pageDTO.getQueryString();
        if(StringUtils.hasLength(query)){
            example.or().andCodeEqualTo(query);
            example.or().andNameEqualTo(query);
        }

        //3.指向分页查询
        List<Checkitem> list = checkitemMapper.selectByExample(example);
        return new PageResult(page.getTotal(),list);
    }


    //新增检查项
    @Transactional
    public void add(Checkitem checkitem) {
        checkitemMapper.insertSelective(checkitem);
    }

    //通过检查项ID查询信息
    @Transactional(readOnly = true)
    public Checkitem viewOne(Integer id) {
        Checkitem checkitem = checkitemMapper.selectByPrimaryKey(id);
        return checkitem;
    }

    //修改检查项信息
    @Transactional
    public void update(Checkitem checkitem) {
        checkitemMapper.updateByPrimaryKeySelective(checkitem);
    }

    //根据检查项ID删除
    @Transactional
    public void delete(Integer id) {
        //封装查询条件
        CheckgroupCheckitemExample example = new CheckgroupCheckitemExample();
        CheckgroupCheckitemExample.Criteria criteria = example.createCriteria();
        criteria.andCheckitemIdEqualTo(id);
        //根据检查项的主键，在中间关系表checkgroup_checkitem中查询记录 select count(*) from checkgroup_checkitem where checkitem_id = 28;
        int count = checkgroupCheckitemMapper.countByExample(example);
        if(count>0){
            //此检查项对应的中间关系表中有记录，则不删除
            throw new RuntimeException("此检查项被占用！");
        }
        //此检查项对应的中间关系表中没有记录，则删除
        checkitemMapper.deleteByPrimaryKey(id);
    }


}
